package droideep.library.widget.menu;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Build;
import android.text.style.MetricAffectingSpan;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import droideep.library.R;
import droideep.library.utils.MetricsConverter;
import droideep.library.widget.FloatingActionButton;

/**
 * 实现类似<a href="https://path.com">Path 2.0</a>效果的
 * ,并且遵守<a href="http://design.1sters.com/">Material Design 中文版</a>的自定义控件
 *
 * @author bri
 * @version 0.1
 * @date 15-5-23.
 * @since Android API 8
 */
public class FloatingMenu extends ViewGroup {
    private final String TAG = FloatingMenu.class.getSimpleName();

    private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

    public FloatingMenu(Context pContext) {
        super(pContext);
    }

    public FloatingMenu(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public FloatingMenu(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public FloatingMenu(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

//        measureChildren(widthMeasureSpec, heightMeasureSpec);
//        setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        if (!changed) return;

        final int _Width = getMeasuredWidth();
        final int _Height = getMeasuredHeight();

        Log.v(TAG, String.format("l:%s,t:%s,r:%s,b:%s", l, t, r, b));

        final int _Count = getChildCount();
        for (int i = 0; i < _Count; i++) {
            View view = getChildAt(i);
            if (view instanceof FloatingActionButton) {
                final int _CWidth = view.getMeasuredWidth();
                final int _CHeight = view.getMeasuredHeight();
                MarginLayoutParams lp = (MarginLayoutParams) view.getLayoutParams();
                final int _CLeft = _Width - _CWidth - lp.leftMargin - lp.rightMargin;
                final int _CTop = _Height - _CHeight - lp.topMargin - lp.bottomMargin;
                final int _CRight = _CLeft + _CWidth;
                final int _CBottom = _CTop + _CHeight;

                view.layout(_CLeft, _CTop, _CRight, _CBottom);
            }
        }
    }

    // 主菜单圆心X坐标
    float mMCenterX;
    // 主菜单圆心Y坐标
    float mMCenterY;
    // 主菜单半径
    final float mMenuRadius = MetricsConverter.dpToPx(getContext(), 28);

    // 菜单选项(Item)圆心X坐标
    double mICenterX;
    // 菜单选项(Item)圆心Y坐标
    double mICenterY;
    // 菜单选项半径
    final float mItemRadius = MetricsConverter.dpToPx(getContext(), 20);

    // 圆弧的半径(主菜单的圆心和菜单的圆心之间的距离)
    final float mRadius = MetricsConverter.dpToPx(getContext(), 120);

    // 菜单的总个数
    final int mItemCount = 5;

    // 圆弧的角度
    final int mItemsAngle = 90;

    @Override
    protected void onDraw(Canvas canvas) {
        final int _Left = 0;
        final int _Top = 0;
        final int _Width = getMeasuredWidth();
        final int _Height = getMeasuredHeight();

        // 画主菜单
        resetPaint();
        mMCenterX = _Width - mMenuRadius;
        mMCenterY = _Height - mMenuRadius;
        mPaint.setColor(getResources().getColor(R.color.material_red_500));
        canvas.drawCircle(mMCenterX, mMCenterY, mMenuRadius, mPaint);

        // 画菜单,即圆弧上的小圆
        resetPaint();
        mPaint.setColor(getResources().getColor(R.color.material_blue_500));
        for (int i = 0; i < mItemCount; i++) {
            final double _Arc = i * Math.PI / (2 * (mItemCount - 1));
            Log.v(TAG, Double.toString(_Arc));
            mICenterX = _Width - (mMenuRadius + mRadius * Math.cos(_Arc));
            mICenterY = _Height - (mMenuRadius + mRadius * Math.sin(_Arc));

            canvas.drawCircle((float) mICenterX, (float) mICenterY, mItemRadius, mPaint);
        }

    }

    /**
     * <p>重置画笔</p>
     *
     * @author goodev
     * @date 15-5-23
     * @version 0.1
     */
    private void resetPaint() {
        mPaint.reset();
        mPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
    }

    @Override
    public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new MarginLayoutParams(getContext(), attrs);
    }
}
